* Table A3a-g First stage regressions 2007-2013
* Sample used to study investments and agricultural outcomes
* Last Updated, Niriksha Shetty 03/31/206

clear
clear mata
clear matrix
set maxvar 20000
set more off

pwd
cap cd c(`pwd')

estimates clear

use "../dta/reduced-form-master.dta", clear

* using balanced_panel
keep if bal_panel3==1

estimates clear

/* LHS=(1)s_policy_units; (2)bought; RHS= full list of instruments. Run by year*/

** individual level treatment checks
replace flyer_exp=flyer_exp|flyer_hyv_exp
replace flyer_hyv=flyer_hyv|flyer_hyv_exp
cap drop discount_video
gen discount_video=discount*video
cap drop fourbdmperc
gen onebdmperc=bdmperc*disc1game
gen fourbdmperc=bdmperc*disc4game

replace discount = 0 if treat_year==0 

/* RHS: Instruments by year, with the year being the year of insurance purchasing for which we are instrumenting */
local inst2013 " bdmperc disc1game disc4game fourbdmperc onebdmperc assigned_risk_ws" // onebdmperc gets omitted
 
local inst2012 " bdmperc disc1game disc4game fourbdmperc onebdmperc assigned_risk_ws" // onebdmperc gets omitted

local inst2011 " bdmperc disc1game disc4game fourbdmperc onebdmperc assigned_risk_ws" // onebdmperc gets omitted

local inst2010 "bdmperc disc1game disc4game fourbdmperc onebdmperc assigned_video_test assigned_drought_flyer assigned_rain assigned_subsidies_flyer assigned_risk_ws assigned_loan" // onebdmperc and assigned_rain get omitted

local inst2009 "bdmperc disc1game  disc4game fourbdmperc onebdmperc" // onebdmperc gets omitted

local inst2008 "discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv assigned_risk_ws" // flyer_hyv==1 if flyer_hyv==1|flyer_exp_hyv==1 (i.e isolates people who got the hyv flyer)

local inst2007  "hinduT muslimT groupT ppayT vframeT discount video" // peerT sewaT pframeT get omitted

* local inst2006 = "mrkt_allnegative mrkt_poslang mrkt_posimg" // DROP 2006 INSTRUMENTS

local uniqinst "bdmperc fourbdmperc onebdmperc disc4game assigned_risk_ws assigned_video_test assigned_drought_flyer assigned_subsidies_flyer assigned_loan assigned_rain discount rebate_50percentoff rebate2_1free rebate3_1free flyer_hyv groupT muslimT hinduT sewaT peerT ppayT vframeT pframeT " /* mrkt_allnegative mrkt_poslang mrkt_posimg*/

*For the panel regressions, I have to zero instruments wherever they are missing.
*This will happen for all control villages as well as for years where certain instruments do not exist

	foreach var of varlist `uniqinst' resurveyed lost_w3 firstt_2007 {
	replace `var' = 0 if `var'==.
	replace `var' = 0 if wave==0
	}


 /// First stage regressions ///

local ovar "s_policy_units bought"


	*** 1 - RUN YEAR BY YEAR

tsset, clear
set seed 2015


forvalues year= 2007/2013 { 
foreach var in `ovar' { 
* Village level
reg `var' treat_year if mkt_year==`year', vce(bootstrap, cluster(villageno) reps(100))
est store f_vill_`var'_y`year'
test  treat_year 
estadd scalar pval = r(p)

* Individual level
reg `var' `inst`year'' if mkt_year==`year', vce(bootstrap, cluster(villageno) reps(100) )
est store f_ind_`var'_y`year'
test `inst`year''
estadd scalar pval = r(p)

}
}

forvalues year= 2007/2013 { 
esttab  f_vill_s_policy_units_y`year' f_ind_s_policy_units_y`year' f_vill_bought_y`year' f_ind_bought_y`year' using "../output/firststage_`year'.csv", replace  ///
	label  b(2) se(2) ///
    eqlabels(none) alignment(S S) ///
	keep(treat_year `inst`year'') ///
	star(* 0.10 ** 0.05 *** 0.01) ///
 	stats(pval N, fmt(2 0) labels("P-value for test of joint significance""Observations")) ///
 	nonote mtitles("Village""Individual""Village""Individual") ///
    mgroups("y = Number of policies purchased" "y = Purchased Insurance"  , pattern(1 0 1 0) )
}
